<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class ApiCheck
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $params = $request->except('s');
        if(!isset($params['sign'])){
            return  response()->json(['status'=>'201','msg'=>'非法接口请求']);
        }
        $sign = $params['sign'];
        //生成签名数据
        $newSign = createSign($params);
        if($sign!=$newSign) {
            return  response()->json(['status'=>'201','msg'=>'签名参数异常']);
        }

        if(Redis::get($sign)){
            return response()->json(['code'=>201,'msg'=>'请勿重复请求接口']);
        }
        Redis::setex($sign,60,1);

        return $next($request);
    }
}
